package com.dream.mqpushclient.netty;

import com.alibaba.fastjson.JSON;
import com.dream.mqpushserver.api.HelloService;
import com.dream.mqpushserver.common.RequestCommand2;
import com.dream.mqpushserver.common.RpcRequest;
import com.dream.mqpushserver.handler.ClientHandler;
import com.dream.mqpushserver.handler.ClientRequestCommandHandler;
import com.dream.mqpushserver.handler.ServerHandler;
import com.dream.mqpushserver.netty.*;
import com.dream.mqpushserver.netty2.RpcDeCoder;
import com.dream.mqpushserver.netty2.RpcEnCoder;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import org.springframework.context.SmartLifecycle;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.UUID;

/**
 * @author song_
 * @Date 2020/3/26
 * @desc
 **/
@Component
public class NettyClient implements SmartLifecycle {

    private static EventLoopGroup group = null;

    private static Channel channel = null;

    @PostConstruct
    public void init() {
        group = new NioEventLoopGroup();
    }

    @Override
    public void start() {
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(group)
                .channel(NioSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 128)
                .option(ChannelOption.SO_KEEPALIVE, false)
                .option(ChannelOption.SO_SNDBUF, 512)
                .option(ChannelOption.SO_RCVBUF, 512)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline pipeline = ch.pipeline();
//                        pipeline.addLast(new MyProtocolDeCoder(1024*1024,1,4));
//                        pipeline.addLast(new MyProtocolEnCoder());
//                        pipeline.addLast(new DeCoder());
//                        pipeline.addLast(new EnCoder());
//                        pipeline.addLast(new ClientRequestCommandHandler());

                        pipeline.addLast(new RpcDeCoder(65565,1,4));
                        pipeline.addLast(new RpcEnCoder());
                        pipeline.addLast(new NettyClientHandler2());
                    }
                });
        ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 10001);
        channel = channelFuture.channel();
    }

    @Override
    public void stop() {
        if (group != null) {
            group.shutdownGracefully();
        }
    }

    @Override
    public boolean isRunning() {
        return false;
    }

    public String sendMsg(RequestCommand command) {
        if (channel != null) {
            channel.writeAndFlush(command);
            return "success";
        }
        return "false";
    }

    public void sendRequestMsg(Request request) {
        if (channel != null)
            channel.writeAndFlush(request);
    }

    public void sendHelloService(String name) {
        RequestCommand2 requestCommand2 = new RequestCommand2() ;
        RpcRequest request = new RpcRequest() ;
        request.setClassName(HelloService.class.getName());
        request.setId(UUID.randomUUID().toString());
        request.setMethodName("sayHello");
        request.setArgs(new Object[]{name});
        request.setParameterTypes(new Class[]{String.class});
        requestCommand2.setType((byte)1);
        requestCommand2.setLength(JSON.toJSONString(request).getBytes().length);
        requestCommand2.setBytes(JSON.toJSONString(request).getBytes());
        channel.writeAndFlush(requestCommand2);
    }
}
